/**
 * @author jaanush
 */
dojo.provide("slipstream.data.JsonRestStoreTrans");
dojo.require("dojox.data.JsonRestStore");

dojo.declare("slipstream.data.JsonRestStoreTrans", [dojox.data.JsonRestStore], {
	getValue: function(/*Object*/ item, /*String*/property, /*value?*/defaultValue){
		// summary:
		//	Gets the value of an item's 'property'
		//
		//	item:
		//		The item to get the value from
		//	property:
		//		property to look up value for
		//	defaultValue:
		//		the default value
		
		var byPath=property[0]==='#';
		property= byPath?property.replace('{lang}',this.lang).substring(1):property;
		var value=item;
		dojo.forEach(property.split('.'),function(attr,idx,path){
			if(value===undefined || value===null) return;
			if (value[attr]===undefined && value._loadObject) {
				//console.log('Loading object on demand',value,attr);
				dojox.rpc._sync = true;
				value=dojox.data.ServiceStore.prototype.loadItem({item:value});
			}
			value=value[attr];
		},this)
		return value || // return the plain value since it was found;
					(property in item ? // a truthy value was not found, see if we actually have it
						value : // we do, so we can return it
						item._loadObject ? // property was not found, maybe because the item is not loaded, we will try to load it synchronously so we can get the property
							(dojox.rpc._sync = true) && arguments.callee.call(this,dojox.data.ServiceStore.prototype.loadItem({item:item}) || {}, property, defaultValue) : // load the item and run getValue again
							defaultValue);// not in item -> return default value
	},
	_getItemByPath: function(_path,_item){
		var _attr=_path.shift();
		
	},
	_getValueByPath: function(item,property){
		return dojo.getObject(property.replace('{lang}',this.lang).substring(1),false,item);
	},
	hasAttribute: function(item,attribute){
		return attribute[0]==='#' ? this._getValueByPath(item,attribute)!=undefined : (attribute in item);
	},
	setValue: function(item, attribute, value){
		// summary:
		//		sets 'attribute' on 'item' to 'value'
		var byPath=attribute[0]==='#';
		attribute= byPath?attribute.replace('{lang}',this.lang).substring(1):attribute;
		var _item=item;
		dojo.forEach(attribute.split('.'),function(attr,idx,path){
			/*if(dojox.json.schema && store.schema && store.schema.properties){
				// if we have a schema and schema validator available we will validate the property change
				dojox.json.schema.mustBeValid(dojox.json.schema.checkPropertyChange(value,store.schema.properties[attribute]));
			}*/
			if (attr in _item && idx!=path.length-1) {
				_item=_item[attr];
				return;
			}
			var store = _item.__id ? dojox.data._getStoreForItem(_item) : this;
			var old = item[attr];
			if(idx==0 && attr == store.idAttribute){
				throw new Error("Can not change the identity attribute for an item");
			}
			store.changing(_item);
			_item[attr]=(idx==path.length-1?_value=value:_value={parent:_item});
			if(value && !value.__parent){
				value.__parent = _item;
			}
			store.onSet(item,attr,old,_value);
			_item=_value;
			
		},this);
	},
});